package com.lemon.core.mybatis.util;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lemon.core.mybatis.annotation.QueryField;
import com.lemon.core.base.util.CheckUtils;
import lombok.experimental.UtilityClass;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @author admin
 * @since 2021-09-03
 */
@UtilityClass
public class QueryWrapperUtils {

    /**
     * 构建查询条件
     *
     * @param obj 数据实体
     * @return void 返回参数说明
     * @exception/throws
     */
    public static <T> QueryWrapper<T> buildQuery(Object obj) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        convertQuery(wrapper, obj);
        return wrapper;
    }

    /**
     * 拼接查询条件
     *
     * @param wrapper 条件对象
     * @param obj 数据实体
     * @return void 返回参数说明
     * @exception/throws
     */
    public static <T> void convertQuery(QueryWrapper<T> wrapper, Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();
        try {
            // 反射遍历属性
            for (Field field : fields) {
                // 抑制Java对修饰符的检查
                field.setAccessible(true);
                // 获取属性值
                Object fieldValue = field.get(obj);
                if (ObjectUtils.isEmpty(fieldValue) || Objects.equals("serialVersionUID",
                    field.getName())) {
                    continue;
                }
                // 查询注解
                QueryField queryField = AnnotationUtils.getAnnotation(field, QueryField.class);
                if (ObjectUtils.isEmpty(queryField)) {
                    wrapper.eq(StringUtils.camelToUnderline(field.getName()), fieldValue);
                    continue;
                }
                if (queryField.ignore()) {
                    continue;
                }
                String fieldName = CheckUtils.isNotBlank(queryField.field()) ? queryField.field()
                    : StringUtils.camelToUnderline(field.getName());
                if (Number.class.isAssignableFrom(field.getType())) {
                    wrapper.eq(fieldName, fieldValue);
                } else {
                    // 获取枚举，拼接查询条件
                    switch (queryField.conditionType()) {
                        case NE:
                            wrapper.ne(fieldName, fieldValue);
                            break;
                        case LIKE:
                            wrapper.like(fieldName, fieldValue);
                            break;
                        case LEFT_LIKE:
                            wrapper.likeLeft(fieldName, fieldValue);
                            break;
                        case RIGHT_LIKE:
                            wrapper.likeRight(fieldName, fieldValue);
                            break;
                        case NOT_LIKE:
                            wrapper.notLike(fieldName, fieldValue);
                            break;
                        case IN:
                            wrapper.in(fieldName, fieldValue);
                            break;
                        case NOT_IN:
                            wrapper.notIn(fieldName, fieldValue);
                            break;
                        default:
                            wrapper.eq(fieldName, fieldValue);
                            break;
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取属性名
     *
     * @exception/throws
     */
    private static String getFieldValue(Object owner, String fieldName) {
        try {
            return ReflectUtil.invoke(owner, fieldName).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
